package com.avocado.base.validator.role;


import com.avocado.base.repository.RoleRepository;
import lombok.extern.slf4j.Slf4j;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

@Slf4j
public class RoleAddCodeValidator implements ConstraintValidator<RoleAddCodeValidation, String> {


    private final RoleRepository roleRepository;

    public RoleAddCodeValidator(RoleRepository roleRepository) {

        this.roleRepository = roleRepository;
    }


    @Override
    public void initialize(RoleAddCodeValidation constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {

        try {

            long count=roleRepository.getCountByCode(value);

            return count<1L;

        } catch (Exception ex) {
            log.error(ex.getLocalizedMessage());
        }
        return false;
    }
}
